package cn.yj.common.lock.redis;

import cn.yj.commons.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPubSub;

/**
 * @author 永健
 * @since 2022-01-26 14:26
 */
public class RedisKeyPubSub extends JedisPubSub {

    private static final Logger log=LoggerFactory.getLogger(RedisKeyPubSub.class);

    private volatile String message;
    private volatile String channel;

    @Override
    public void onMessage(String channel, String message) {
        this.message = message;
        this.channel = channel;
         log.info("收到锁释放信号:{}:{}",channel,message);
    }

    public String get() {
        while (StringUtils.isBlank(this.message)) {
        }
        // 取消订阅
        super.unsubscribe(this.channel);
        return this.message;
    }
}
